<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *员工列表
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
$this->session('juese',15);
$return['user_juese']=$this->user_juese;
$title=$return['user_juese'][$this->session('juese')];
$tabs=array(
	array('op'=>'list','title'=>$title.'列表'),
	array('op'=>'edit','title'=>'添加'.$title),
);
//员工列表
if($_RQ['op']=='list'){
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$return['params']=array();
	$params=array('uniacid'=>$_SESSION['uniacid'],'juese'=>$this->session('juese'));
	if($_RQ['status']){
		$params['status']=intval($_RQ['status']);
		$return['params']['status']=$_RQ['status'];
	}
	if($_RQ['storeid']){
		$params['storeid']=intval($_RQ['storeid']);
		$return['params']['storeid']=$_RQ['storeid'];
	}
	if(!empty($_RQ['title'])){
		$params['title like']='%'.$_RQ['title'].'%';
		$return['params']['title']=$_RQ['title'];
	}
	if(!empty($_RQ['mobile'])){
		$params['mobile']=$_RQ['mobile'];
		$return['params']['mobile']=$_RQ['mobile'];
	}
	$return['params']['psize']=intval($_RQ['psize']);
	$pindex = max(1, intval($_RQ['page']));
	$psize = $_RQ['psize']?:10;
	$list=pdo_getslice('jy_yxw_worker',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$item['member']=cfc('core')->member_show($item['mid']);
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	//页面显示设置
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'export'=>'export',
		'list'=>array('data'=>'list','tip'=>'[温馨提示]：
		</br>1、这里显示所有门店的员工！
		','del_message'=>'确认删除该员工吗？','total'=>$total,'psize'=>$psize,'del_op'=>'del','html_to'=>'list_html','list'=>array(
			array('type'=>'member','title'=>'会员信息','name'=>'member'),
			array('type'=>'string','title'=>'姓名','name'=>'title'),
			array('type'=>'string','title'=>'手机号','name'=>'mobile'),
			array('name'=>'gangwei','title'=>'岗位名称','type'=>'string'),
			array('type'=>'string','title'=>'登录账号','name'=>'username'),
			array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status'),
			array('name'=>'createtime','title'=>'添加日期','type'=>'time'),
		),'action'=>array(
			array('type'=>'model','op'=>'setstore','title'=>'修改门店'),
		),'batch'=>$batch),
		'search'=>array(
			array('type'=>'string','width'=>'15','title'=>'手机号','name'=>'mobile','help'=>''),
			array('type'=>'string','width'=>'15','title'=>'员工名称','name'=>'title','help'=>''),
			array('name'=>'status','width'=>'15','title'=>'状态','type'=>'radio','options'=>'status'),
		),
	);
	$return['stores']=$this->getall('store',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$return['status']=$this->worker_status;
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='edit'){
	$return=array();
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_yxw_worker',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$item=array();
	}
	$item['password']='';
	$return['item']=$item;
	$return['item']['createtime']=date('Y-m-d H:i:s',$item['createtime']);
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回列表'),
		),
		'edit'=>array(
					array('type'=>'string','title'=>'姓名','name'=>'title','help'=>''),
					array('type'=>'image','title'=>'头像','name'=>'logo','help'=>''),
					array('name'=>'storeidlist','title'=>'管理门店','type'=>'checkbox_select','options'=>'stores'),
					array('name'=>'gangwei','title'=>'岗位名称','type'=>'string'),
					array('name'=>'mid','title'=>'绑定系统会员','type'=>'choose','action'=>'core/member.get_member/'),	
					array('type'=>'string','title'=>'登录账号','name'=>'username','help'=>''),
					array('type'=>'password','title'=>'登录密码','name'=>'password','help'=>''),
					array('type'=>'string','title'=>'手机号','name'=>'mobile','help'=>''),
					array('type'=>'radio','title'=>'性别','name'=>'sex','options'=>'sex'),
					array('type'=>'time','title'=>'生日','name'=>'birth','help'=>''),
					array('type'=>'time','title'=>'入职时间','name'=>'add_days','help'=>''),
					array('type'=>'pcd','title'=>'户籍','name'=>'pcd','help'=>''),
					array('type'=>'string','title'=>'详细地址','name'=>'address','help'=>''),
					array('type'=>'radio','title'=>'状态','name'=>'status','options'=>'status'),
		)
	);
	$return['sex']=$this->sex;
	$return['status']=$this->worker_status;
	$return['stores']=$this->getall('store',array('uniacid'=>$_SESSION['uniacid'],'status'=>1),array('id','title'));	
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	if(!$this->session('juese')){
		exi('操作异常！','error');
	}
	if(!$postdata['title']){
		exi('请填写姓名！','error');
	}
	if(!$postdata['storeidlist']){
		//exi('请选择管理门店！','error');
	}
	$data=array(
		'title'=>$postdata['title'],
		'gangwei'=>$postdata['gangwei'],
		'logo'=>$postdata['logo'],
		'mobile'=>$postdata['mobile'],
		'add_days'=>$postdata['add_days'],
		'storeidlist'=>$postdata['storeidlist'],
		'sex'=>intval($postdata['sex']),
		'birth'=>($postdata['birth']),
		'pcd'=>ijson_encode($postdata['pcd']),
		'address'=>$postdata['address'],
		'username'=>$postdata['username'],
		'status'=>intval($postdata['status']),
		'mid'=>intval($postdata['mid']),
	);
	$id=intval($postdata['id']);
	if($postdata['password']){
		$data['password']=base64_encode($postdata['password']);
	}
	if($data['username']){
		$uname=pdo_get('jy_yxw_worker',array('username'=>$data['username'],'uniacid'=>$_SESSION['uniacid']));
	}
	if($uname && $uname['id']!=$id){
		exi('账号不可以重复','error');	
	}
	
	if($id){
		pdo_update('jy_yxw_worker',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		if(!$postdata['username'] || !$postdata['password']){
			exi('请填写账户和密码！','error');
		}
		$data['uniacid']=$_SESSION['uniacid'];
		$data['juese']=$this->session('juese');
		$data['createtime']=time();
		pdo_insert('jy_yxw_worker',$data);
	}
	exi('操作成功！','','list');
}
if($_RQ['op']=='export'){
	//员工列表
	
	$return['params']=array();
	$params=array('uniacid'=>$_SESSION['uniacid'],'juese'=>$this->session('juese'));
	if($_RQ['status']!='all'){
		$params['status']=intval($_RQ['status']);
	}
	$return['params']['status']=$_RQ['status'];
	if($_RQ['storeid']){
		$params['storeid']=intval($_RQ['storeid']);
		$return['params']['storeid']=$_RQ['storeid'];
	}
	if(!empty($_RQ['title'])){
		$params['title like']='%'.$_RQ['title'].'%';
		$return['params']['title']=$_RQ['title'];
	}
	if(!empty($_RQ['mobile'])){
		$params['mobile']=$_RQ['mobile'];
		$return['params']['mobile']=$_RQ['mobile'];
	}
	$list=pdo_getall('jy_yxw_worker',$params,$fields);
	if($list){
		foreach($list as &$item){
			$item['juesetitle']=$this->user_juese[$item['juese']];
			$item['statustitle']=$this->worker_status[$item['status']];
			$item['createtime']=date('Y/m/d H:i:s',$item['createtime']);
		}
	}
	$header=array(
		'id'=>'员工ID',
		'title'=>'姓名',
		'mobile'=>'手机号',
		'juesetitle'=>'岗位',
		'statustitle'=>'状态',
		'createtime'=>'添加时间',
	);
	model('core/tools')->export2excel($header,$list,'员工数据');
}

if($_RQ['op']=='setstore'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$ids=$post['ids'];
		foreach($ids as $id){
			$id=intval($id);
			if($id){
				pdo_update('jy_yxw_worker',array('storeidlist'=>$post['storeidlist']),array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
			}
		}
		exi('操作成功！','','//list/');
	}
	$ids=trim($_RQ['ids'],';');
	if($ids){
		$ids=explode(';',$ids);
	}else{
		$ids=array();
	}
	if($_RQ['id']){
		$ids[]=$_RQ['id'];
		$item=pdo_get('jy_yxw_worker',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']),array('storeidlist','id'));
	}
	if(count($ids)==0){
		exi('请先选择员工','error');
	}
	$item['ids']=$ids;
	$return['item']=$item;	
	$htmls=array(
		'type'=>'edit',
		'desc'=>'管理门店',
	);
	$htmls['edit']=array(
		array('name'=>'storeidlist','title'=>'管理门店','type'=>'checkbox_select','options'=>'stores'),
	);
	$return['htmls']=$htmls;
	$return['stores']=$this->getall('store',array('uniacid'=>$_SESSION['uniacid'],'status'=>1),array('id','title'));	
	exi($return);
}
if($_RQ['op']=='del'){
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			pdo_delete('jy_yxw_worker',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('删除成功！');
}